﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using BibMan.util;

namespace BibMan.gui.util {
    public delegate void labelUpdatedHandler(object sender, String oldText, String newText);
    public class LabelPanel : Label {

        /// <summary>
        /// Event thrown when text of this label is changed
        /// </summary>
        public event labelUpdatedHandler TextChanged;


        public override String Text {
            get {
                return base.Text;
            }
            set {
                String old = Text;
                //this.Name = value;
                base.Text = value;
                textBox.Text = value;
                textBox.TextAlign = HorizontalAlignment.Right;
                if (TextChanged != null) TextChanged(this, old, value);
            }
        }
        //public ContentAlignment TextAlign { 
        //    get {
        //        return label.TextAlign;
        //    }
        //    set {
        //        label.TextAlign = value;
        //    }
        //}

       // private Label label;
        private TextBox textBox;
        public LabelPanel()
            : base() {
           
                this.AutoSize = true;
               // this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
             //   this.Size = new Size(21, 21);
                //label = new Label();
                this.Dock = DockStyle.Fill;
                this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                this.MouseClick += labelClicked;
              //  this.Controls.Add(label);

                textBox = new TextBox();
                textBox.Dock = DockStyle.Fill;
                textBox.Visible = false;
                textBox.KeyDown += enterPressed;     
                this.Controls.Add(textBox);


                textBox.LostFocus += lostFocus;
        }

        public void BeginEdit() {
            textBox.Visible = true;
            textBox.Focus();
        }

        private void labelClicked(object sender, MouseEventArgs e) {
            textBox.Visible = true;
            textBox.Focus();
        }

        private void enterPressed(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Return) {
                Text = textBox.Text;
                textBox.Visible = false;
            }
        }

        private void lostFocus(object sender, EventArgs e) {
            Text = textBox.Text;
            textBox.Visible = false;
        }

        
    }
}
